import { LoadingUtil } from './../../common/loading';
import { UiserviceProvider } from './../../providers/uiservice/uiservice';
import { BusiserviceProvider } from './../../providers/busiservice/busiservice';
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, LoadingController } from 'ionic-angular';
import { Utils } from '../../common/utils';

/**
 * Generated class for the RegiPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-regi',
  templateUrl: 'regi.html',
})
export class RegiPage {
  sysuser={
    username:"",
    password:"",
    type:"1",
    name:"",
    address:"",
    phone:""
  }
  loading:LoadingUtil;
  confirmPassword:any="";
  constructor(public navCtrl: NavController, public navParams: NavParams,public busi: BusiserviceProvider
    , private uiservice: UiserviceProvider,private loadingCtrl: LoadingController) {
      this.loading = new LoadingUtil(this.loadingCtrl);
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad RegiPage');
  }
  save(){
    if(Utils.isNullOrEmpty(this.sysuser.username)){
      alert("用户名为空!");
      return;
    }
    if(Utils.isNullOrEmpty(this.sysuser.password)){
      alert("密码为空!");
      return;
    }
    if(this.sysuser.password !=this.confirmPassword){
      alert("两次输入密码不一致!");
      return;
    }
    if(Utils.isNullOrEmpty(this.sysuser.name)){
      alert("店铺名称为空!");
      return;
    }
    if(Utils.isNullOrEmpty(this.sysuser.phone)){
      alert("店铺电话为空!");
      return;
    }
    if(Utils.isNullOrEmpty(this.sysuser.address)){
      alert("店铺地址为空!");
      return;
    }
    this.loading.showLoading();
    this.busi.regi(this.sysuser).subscribe(
      data => {
        this.loading.hideLoading();
        if (data._success) {
          this.uiservice.showToast("注册成功!");
          this.navCtrl.pop();
        } else {
          this.uiservice.showAlert(data._message);
        }
      },
      error => {
        this.loading.hideLoading();
        this.uiservice.showAlert("网络异常!");
      }
    );
  }

}
